#ifndef __W25QXX_H
#define __W25QXX_H

#include <stdio.h>
#include "stm32f10x.h"

/*
    #define  sFLASH_ID                    0xEF3015   //W25QXX16
    #define  sFLASH_ID                    0xEF4015	 //W25Q16
    #define  sFLASH_ID                    0XEF4017   //W25Q64
*/
#define sFLASH_ID                         0XEF4018    

#define W25QXX_PageSize                256
#define W25QXX_PerWritePageSize        256

/*命令定义-开头*******************************/
#define W25QXX_WriteEnable		              0x06 
#define W25QXX_WriteDisable		              0x04 
#define W25QXX_ReadStatusReg		            0x05 
#define W25QXX_WriteStatusReg		            0x01 
#define W25QXX_ReadData			                0x03 
#define W25QXX_FastReadData		              0x0B 
#define W25QXX_FastReadDual		              0x3B 
#define W25QXX_PageProgram		              0x02 
#define W25QXX_BlockErase			              0xD8 
#define W25QXX_SectorErase		              0x20 
#define W25QXX_ChipErase			              0xC7 
#define W25QXX_PowerDown			              0xB9 
#define W25QXX_ReleasePowerDown	            0xAB 
#define W25QXX_DeviceID			                0xAB 
#define W25QXX_ManufactDeviceID   	        0x90 
#define W25QXX_JedecDeviceID		            0x9F

/* WIP(busy)标志，FLASH内部正在写入 */
#define WIP_Flag                          0x01
#define Dummy_Byte                        0xFF


/*等待超时时间*/
#define SPIT_FLAG_TIMEOUT                 ((uint32_t)0x1000)
#define SPIT_LONG_TIMEOUT                 ((uint32_t)(10 * SPIT_FLAG_TIMEOUT))

/*信息输出*/
#define FLASH_DEBUG_ON                    1

#define FLASH_INFO(fmt,arg...)            printf("<<-FLASH-INFO->> "fmt"\n",##arg)
#define FLASH_ERROR(fmt,arg...)           printf("<<-FLASH-ERROR->> "fmt"\n",##arg)
#define FLASH_DEBUG(fmt,arg...)           do{\
                                            if(FLASH_DEBUG_ON)\
                                            printf("<<-FLASH-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
                                          }while(0)

void W25QXX_Initialization(void);
void W25QXX_SectorEraseFun(u32 SectorAddr);
void W25QXX_AllErase(void);
void W25QXX_PageWrite(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
void W25QXX_BufferWrite(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
void W25QXX_BufferRead(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);
u32 W25QXX_ReadID(void);
u32 W25QXX_ReadDeviceID(void);
void W25QXX_StartReadSequence(u32 ReadAddr);
u8 W25QXX_ReadByte(void);
u8 W25QXX_SendByte(u8 byte);
u16 W25QXX_SendHalfWord(u16 HalfWord);
void W25QXX_WriteEnableFun(void);
void W25QXX_WaitForWriteEnd(void);
void W25QXX_PowerDownFun(void);
void W25QXX_WAKEUP(void);

#endif /* __W25QXX_H */